package com.xlab.xlabtestapp.provider;

import com.xlab.xlabtestapp.ResourceTable;
import com.xlab.xlabtestapp.utils.LogUtil;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class ContactProvider<T> extends BaseItemProvider {
    private final static String TAG = ContactProvider.class.getSimpleName();
    private final Context mContext;
    private final List<T> mList;

    public ContactProvider(Context context, List<T> listBean) {
        mContext = context;
        mList = listBean;
    }
    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public T getItem(int idx) {
        return (mList == null || mList.size() == 0) ? null : mList.get(idx);
    }

    @Override
    public long getItemId(int idx) {
        return idx;
    }

    @Override
    public Component getComponent(int idx, Component component, ComponentContainer componentContainer) {
        ContactItemViewHolder holder;

        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_contact_item, null, false);
        }

        holder = (ContactItemViewHolder)mList.get(idx);

        if (holder == null) {
            LogUtil.error(TAG, "getComponent[" + idx + "] is null");
            return component;
        }
        Text name = (Text)component.findComponentById(ResourceTable.Id_name);
        name.setText(holder.getName());
        Text num = (Text)component.findComponentById(ResourceTable.Id_number);
        num.setText(holder.getNum());
        Text device = (Text)component.findComponentById(ResourceTable.Id_deviceInfo);
        device.setText(holder.getDevice());
        return component;
    }
}
